<%@ include file="/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="clienteempresaform.estaciones.title"/></title>
    <meta name="heading" content="<fmt:message key='clienteempresaform.estaciones.heading'/>"/>
</head>

<spring:bind path="clienteempresa.*">
    <c:if test="${not empty status.errorMessages}">
    <div class="error">
        <c:forEach var="error" items="${status.errorMessages}">
            <img src="<c:url value="/images/iconWarning.gif"/>"
                alt="<fmt:message key="icon.warning"/>" class="icon"/>
            <c:out value="${error}" escapeXml="false"/><br />
        </c:forEach>
    </div>
    </c:if>
</spring:bind>
<input type="hidden" id = "contadorEstacionesID" name="contadorEstaciones" value="${contadorEstaciones}"  >
<form:form commandName="clienteempresa" method="post" action="clienteempresaestacionform.html" 
			 id="clienteempresaform">
<form:hidden path="id"/>
<ul>
    <li>
     <label class="desc"><fmt:message key="clienteempresaform.title"/></label>
    <div class="group"  style="width: 400px">
	<div>
        <label for="deClienteEmpresa" class="required desc">
        <fmt:message key="clienteempresaform.razonsocial"/>
        </label>
        <form:errors path="deClienteEmpresa" cssClass="fieldError"/>
		<form:hidden path="deClienteEmpresa" id="deClienteEmpresaID"  />
		${clienteempresa.deClienteEmpresa}
    </div>
    
	<div>
        <label for="coRuc" class="required desc">
        <fmt:message key="clienteempresaform.ruc"/>
        </label>
        <form:errors path="coRuc" cssClass="fieldError"/>
		<form:hidden path="coRuc" id="coRucID" />
				${clienteempresa.coRuc}
    </div>    
    
	<div>
        <label for="deAlias" class="required desc">
        <fmt:message key="clienteempresaform.alias"/>
        </label>
        <form:errors path="deAlias" cssClass="fieldError"/>
		<form:hidden path="deAlias" id="deAliasID" />
				${clienteempresa.deAlias}
    </div>       
    </div>
   </li> 
   <li >

        <%-- So the buttons can be used at the bottom of the form --%>
        <c:set var="buttons">
        	<c:if test="${clienteempresa.stClienteEmpresa==true}" >
        	<input type="submit" class="button" name="save" id="bSumit" value="<fmt:message key="button.save"/>"/>
        	</c:if>
            
             
            <c:if test="${param.from == 'list' and param.method != 'Add'}">
			<input type="button" class="button" name="delete"	
					onclick="confirmarEliminarCliente('Cliente', ${clienteempresa.id})"
					value="<fmt:message key="button.delete"/>"/>	                
        	</c:if>
            
            <input type="button"  class="button"  onclick="location.href='<c:url value="/clienteempresa/clienteempresaform.html?id=${clienteempresa.id}&from=list"/>'"
		        value="<fmt:message key="button.done"/>"/>
               
			<input type="button"  class="button"  onclick="location.href='<c:url value="/clienteempresa/clienteempresaform.html?id=${clienteempresa.id}&from=list"/>'"
		        value="<fmt:message key="button.cancel"/>"/>             	                   
        </c:set>        
        <c:out value="${buttons}" escapeXml="false"/>
   </li>

	</ul>
		
<table id="estaciones" cellpadding="0" cellspacing="0" style ="width: 500px" class="table" > 
			<thead> 
				<tr> 
		            <th scope="col" width="30%"><fmt:message key="estacionForm.coCodigoCofide"/></th>
		            <th scope="col" width="60%"><fmt:message key="common.nombre"/></th>
		            
		    		<c:choose>
			    		 <c:when test="${clienteempresa.stClienteEmpresa == false}">
					  </c:when>
						  <c:otherwise>
						<th scope="col" width="10%"><input type="checkbox" id="chkMarcarTodosID" onclick="marcarTodos()"></th>
						  </c:otherwise>
					  </c:choose>
						</tr>
					</thead>
	
			<tbody id="clientesbody">
					<tr id="pattern" style="display:none;">
						<td> 
							<span id="tableCoigoCofide">Codigo Cofide</span>						
						</td>	
						<td> 
							<span id="tableNombre">Nombre</span>						
						</td>
						
		    		<c:choose>
			    		 <c:when test="${clienteempresa.stClienteEmpresa == false}">
					  </c:when>
						  <c:otherwise>
							<td> 
								<span id="tableCheck"></span>						
							</td>
						  </c:otherwise>
					  </c:choose>
					</tr>
					

				        
					<c:if test="${clienteempresa.clienteEstaciones != null}">		
					
						<c:forEach var="estacion" items="${clienteempresa.clienteEstaciones}" varStatus="status">	
							<c:choose>
								  <c:when test="${status.index % 2 == 0}"><tr class="odd" ></c:when>
								  <c:otherwise><tr class="even" ></c:otherwise>
							</c:choose>
				            	<td>
								  ${estacion.coCodigoCofide}
								</td>
								<td>
				                  ${estacion.deEstacion}
				                </td>
				                
         		   		<c:choose>
			    		 <c:when test="${clienteempresa.stClienteEmpresa == false}">
						  </c:when>
						  <c:otherwise>
								<td>
								
								<c:if test="${clienteempresa.stClienteEmpresa==true}" >
									<c:choose>
										<c:when test="${estacion.habilitado eq 'Y'}">
												<input type="checkbox" id="chk_${status.index}" name="chk_${status.index}" checked="checked" onclick="actualizaCheck('${status.index}')" />
										</c:when>
										<c:otherwise>
												<input type="checkbox" id="chk_${status.index}" name="chk_${status.index}" onclick="actualizaCheck('${status.index}')" />
										</c:otherwise>
									</c:choose>	
								</c:if>
								<c:if test="${clienteempresa.stClienteEmpresa!=true}" >
									<c:choose>
										<c:when test="${estacion.habilitado eq 'Y'}">
												<input type="checkbox" id="chk_${status.index}" name="chk_${status.index}" checked="checked" disabled="disabled" />
										</c:when>
										<c:otherwise>
												<input type="checkbox" id="chk_${status.index}" name="chk_${status.index}" disabled="disabled"  />
										</c:otherwise>
									</c:choose>	
								</c:if>
								

								<input type="hidden" maxlength="50" class="text medium" value="${estacion.id}"
                				id="clienteEstaciones[${status.index}].id" name="clienteEstaciones[${status.index}].id" />

								<input type="hidden" maxlength="50" class="text medium" value="${estacion.coEstacion}"
                				id="clienteEstaciones[${status.index}].coEstacion" name="clienteEstaciones[${status.index}].coEstacion" />
                				                				                				
								<input type="hidden" maxlength="50" class="text medium" value="${estacion.habilitado}"
                				id="clienteEstaciones[${status.index}].habilitado" name="clienteEstaciones[${status.index}].habilitado" />
                				                				                				
				                </td>				                
						  
						  </c:otherwise>
					  </c:choose>
				                
				           	</tr>

				        </c:forEach>															
					</c:if>			
					
			</tbody>
				
				 
			</table>	
<c:out value="${buttons}" escapeXml="false"/>				

</form:form>

<script type="text/javascript">


function actualizaCheck(indice){
	if (document.getElementById("chk_" + indice).checked){
		document.getElementById("clienteEstaciones[" + indice+"].habilitado").value ="Y"; 
	} else {
		document.getElementById("clienteEstaciones[" + indice+"].habilitado").value ="N";
	}
}

function marcarTodos(){
	var  contadorEstacionesID = document.getElementById("contadorEstacionesID").value; 
	if (document.getElementById("chkMarcarTodosID").checked){
			for (i = 0 ;i <contadorEstacionesID;i++){
					document.getElementById("chk_"+i).checked  = true;
					actualizaCheck(i);
				}
		} else {
			for (i = 0 ;i <contadorEstacionesID;i++){
				document.getElementById("chk_"+i).checked = false;
				actualizaCheck(i);
				
			}
			
			}
}
</script>
<v:javascript formName="clienteempresa" staticJavascript="false"/>

<script type="text/javascript" src="<c:url value="/scripts/validator.jsp"/>"></script>